package com.wxxymakers.grademark.controller;

import com.wxxymakers.grademark.model.entity.WxDuty;
import com.wxxymakers.grademark.service.WxDutyService;
import com.wxxymakers.grademark.utils.RestResponse;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @AUTHOR soft
 * @DATE 2018/12/24 2:09
 * @DESCRIBE 职位控制器
 */
@RestController
@RequestMapping("/duty")
public class DutyController {

    @Autowired
    private WxDutyService dutyService;

    @PostMapping("/add")
    @RequiresRoles({"admin"})
    public RestResponse add(WxDuty duty) {
        if (dutyService.add(duty)) {
            return RestResponse.ok(duty);
        }
        return RestResponse.fail();
    }

    @PostMapping("/delete/{id}")
    @RequiresRoles({"admin"})
    public RestResponse delete(@PathVariable int id) {
        if (dutyService.delete(id)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    @PostMapping("/change")
    @RequiresRoles({"admin"})
    public RestResponse change(WxDuty duty) {
        if (dutyService.change(duty)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    /**
     * 获取所有职位 不用任何权限 给注册使用
     */
    @PostMapping("/list")
    public RestResponse list() {
        List<WxDuty> list = dutyService.list();
        if (!CollectionUtils.isEmpty(list)) {
            return RestResponse.ok(list);
        }
        return RestResponse.fail();
    }

    @PostMapping("/page")
    public RestResponse page(@RequestParam int page,
                             @RequestParam int limit,
                             WxDuty duty) {
        Page<WxDuty> dutyPage = dutyService.page(page, limit, duty);
        if (dutyPage != null) {
            return RestResponse.ok(dutyPage);
        }
        return RestResponse.fail();
    }
}
